class Person:
  
  def __init__(self, name):
    self.name = name
  
  def changeName(self, name):
    self.name = name

  def showInfo(self):
    print('Hi, my name is {}.'.format(self.name))

Alex = Person('Alex')

Alex.showInfo()
# Hi, my name is Alex.


# ------ hasattr ------
print(hasattr(Alex, 'name'))
# True
print(hasattr(Alex, 'changeName'))
# True
print(hasattr(Alex, 'showInfo'))
# True



# ------ getattr ------
showInfo = getattr(Alex, 'showInfo', 'none-showInfo')
print(showInfo)
# <bound method Person.showInfo of <__main__.Person object at 0x01650F58>>
showInfo()
# Hi, my name is Alex.

showName = getattr(Alex, 'showName', 'none-showName')
print(showName)
# none-showName

# showName()   <--- 如果执行这行代码, 则会报错, 因为Alex这个实例对象没有这属性可以被调用



# ------ setattr ------
# 先定义好一个方法, 然后使用setattr将该方法添加到指定的实例对象中
def sayHi():
  print('你好~')

setattr(Alex, 'sayHi', sayHi)
Alex.sayHi()
# 你好~